package com.itheima.appumeng.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.widget.FrameLayout;

import com.itheima.appumeng.R;
import com.itheima.appumeng.fragment.MainFragment01;
import com.itheima.appumeng.fragment.MainFragment02;
import com.itheima.appumeng.fragment.MainFragment03;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private FrameLayout mContainerLayout;

    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContainerLayout = (FrameLayout) findViewById(R.id.fl_container_layout);

        initFragments();
        initTabLayout();
    }

    private void initFragments() {
        fragments = new ArrayList<>();
        fragments.add(new MainFragment01());
        fragments.add(new MainFragment02());
        fragments.add(new MainFragment03());
    }

    private void initTabLayout() {
        String[] titles = {"消息", "联系人", "动态"};
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        for (int i = 0; i < titles.length; i++) {
            // 添加tab
            tabLayout.addTab(tabLayout.newTab().setText(titles[i]));
        }
        tabLayout.setOnTabSelectedListener(onTabSelectedListener);
        onTabChange(0);
    }

    private void onTabChange(int position) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fl_container_layout, fragments.get(position))
                .commit();
    }

    TabLayout.OnTabSelectedListener onTabSelectedListener
            = new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int position = tab.getPosition();
            onTabChange(position);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    };
   /* //在有framgent 的activit 统计时长  在framgent 统计界面  按文档是这样统计的  可能数据有错  所以我们后来统一写到fragment中去
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);       //统计时长
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }*/
}
